package com.leyou.item.controller;


import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * http://127.0.0.1:9081/category/list?pid=0 路径变量
     * 查询-分类管理
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam("pid") Long pid) {
        List<Category> categories = categoryService.queryByParentId(pid);
        if (categories != null && categories.size() != 0) {
            return ResponseEntity.ok(categories); // 返回页面响应码200及数据
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); // 204, 查询无数据
    }

    /**
     * http://api.leyou.com/api/item/category/bid/325405 请求参数
     * 根据id更新品牌信息
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> categories = categoryService.queryByBrandId(bid);
        if (categories != null && categories.size() != 0) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();// 204, 查询无数据
    }

    /**
     * 根据商品分类id查询名称
     * @param ids 要查询的分类id集合
     * @return 多个名称的集合
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids") List<Long> ids){
        List<String > list = this.categoryService.queryNamesByIds(ids);
        if (null != list || list.size() > 0) {
            return ResponseEntity.ok(list);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
